package server;

import common.*;
import dao.*;
import tools.GetId;

import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;

//服务器
//开启一个线程
public class ClientConnServerThread extends Thread{
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    public Socket s;

    public ClientConnServerThread(Socket s) throws IOException{
        this.s = s;
        oos = new ObjectOutputStream(s.getOutputStream());
        ois = new ObjectInputStream(s.getInputStream());
    }

    public Socket getSocket(){
        return s;
    }
    public void setSocket(){
        this.s = s;
    }

    public void run(){
        while (true){
            try {
                Message msg = (Message)ois.readObject();
                System.out.println(msg.getType());
                if (msg.getType().equals("login")) {
                    //System.out.println("ggg");
                    boolean flag=true;
                    //System.out.println(flag);
                    if(flag) {
                        //msg.setType("loginac");
                        System.out.println(oos);
                        System.out.println(msg.getUser().getId());
                        System.out.println(s);
                        ManageSocket.setSocketHashMap(msg.getUser().getId(),s);
                        ManageSocket.setOis(msg.getUser().getId(),ois);
                        ManageSocket.setOos(msg.getUser().getId(),oos);
                        Message responseMsg = new Message();
                        responseMsg.setType("loginac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    } else {
                        msg.setType("login");
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("close")){
                    Message responseMsg = new Message();
                    responseMsg.setType("closeac");
                    responseMsg.setUser(msg.getUser());
                    oos.writeObject(responseMsg);
                    ManageSocket.setOos(msg.getUser().getId(),null);
                    ManageSocket.setOis(msg.getUser().getId(),null);
                    ManageSocket.setSocketHashMap(msg.getUser().getId(),null);
                    System.out.println("sssb");
                }
//                else if(msg.getType().equals("register")){
//                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
//                    String id = new GetId().getId();
//                    User user=msg.getUser();
//                    user.setId(id);
//                    System.out.println(id);
//                    msg.setUser(user);
//                    //System.out.println(ID);
////                    JOptionPane.showMessageDialog(null,"注册成功！您的账号为：" + id,"提示",JOptionPane.PLAIN_MESSAGE);
//                    boolean flag=new AddUser().adduser(msg.getUser());
//                    if(flag){
//                        Message responseMsg = new Message();
//                        responseMsg.setType("registerac");
//                        responseMsg.setUser(msg.getUser());
//                        oos.writeObject(responseMsg);
//                    }
//                    else{
//                        oos.writeObject(msg);
//                    }
//                }
                else if(msg.getType().equals("checkemail")){
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    boolean flag=false;
                    flag=new CheckEmail().checkEmail(msg.getUser().getEmail());
                    System.out.println("r"+flag);
                    if(flag){
                        oos.writeObject(msg);
                    }
                    else{
                        Message responseMsg = new Message();
                        responseMsg.setType("checkemailac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("getuser")){//获得当前用户信息
                    User us=new GetUser().getUser(msg.getUser());
                    System.out.println("获取到的用户信息:"+us);
                    Message responseMsg = new Message();
                    responseMsg.setUser(us);
                    responseMsg.setType("getuserac");
                    oos.writeObject(responseMsg);
                }else if(msg.getType().equals("getfriend")){//获得当前用户信息
                    User us=new GetUser().getUser(msg.getUser());
                    System.out.println("获取到的用户信息:"+us);
                    Message responseMsg = new Message();
                    responseMsg.setUser(us);
                    responseMsg.setType("getfriendac");
                    oos.writeObject(responseMsg);
                }else if(msg.getType().equals("getgroup")){//获取群聊的信息
                    Group us=new GetGroup().getGroup(msg.getGroup());
                    System.out.println("获取到的群聊信息:"+us);
                    Message responseMsg = new Message();
                    responseMsg.setGroup(us);
                    responseMsg.setType("getgroupac");
                    oos.writeObject(responseMsg);
                }
//                else if(msg.getType().equals("mpwd")){//找回密码中的确认
//                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
//                    if(new modifypwd().mpwd(msg.getUser().getEmail(),msg.getUser().getPsw())){
//                        Message responseMsg = new Message();
//                        responseMsg.setType("mpwdac");
//                        responseMsg.setUser(msg.getUser());
//                        oos.writeObject(responseMsg);
//                    }else{
//                        oos.writeObject(msg);
//                    }
//                }
                else if(msg.getType().equals("changeInformation")){//修改信息
                    //System.out.println(msg.getUser());
                    User tmpuser=msg.getUser();
                    if(msg.getHeadimage()!=null) {
                        //目标路径
                        String filePath = "E:/untitled/src/view/photo/";
                        File upfile = new File(filePath);
                        //如果文件目录不存在，就执行创建
                        if (!upfile.isDirectory()) {
                            upfile.mkdirs();
                        }
                        File localFile = msg.getHeadimage();
                        String filename = new GetId().getId();
                        //目标文件名称
                        String targetName = filename + ".jpg";
                        System.out.println(targetName);
                        //创建目标文件
                        File targetFile = new File(filePath + targetName);
                        FileOutputStream fos = new FileOutputStream(targetFile);
                        //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
                        //获取本地文件输入流
                        InputStream stream = new FileInputStream(localFile);
                        //写入目标文件，字节流
                        byte[] buffer = new byte[1024 * 1024];
                        int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
                        while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
                            //把字节转成String 从0到N变成String
                            fos.write(buffer, 0, byteRead);
                            fos.flush();
                        }
                        //一定要关闭文件流。
                        fos.close();
                        stream.close();
                        System.out.println("关闭上传头像的文件流");
                        tmpuser.setHeadimage(filePath+targetName);
                        msg.setUser(tmpuser);
                    }
                    System.out.println(msg.getUser().getHeadimage());

                    //System.out.println("ss"+msg.getUser().getGender());
                    if(new UpdateInformation().updateInformation(msg.getUser())){
                        Message responseMsg = new Message();
                        responseMsg.setType("changeInformationac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("xgcheckemail")){//检查修改密码的邮箱是否存在
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    boolean flag=false;
                    flag=new CheckEmail().checkEmail(msg.getUser().getEmail());
                    System.out.println(flag);
                    if(flag){
                        oos.writeObject(msg);
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setType("xgcheckemailac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("xgpwd")){//修改密码
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    if(new modifypwd().mpwd(msg.getUser().getEmail(),msg.getUser().getPsw())){
                        Message responseMsg = new Message();
                        responseMsg.setType("xgpwdac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("addfriend")){//添加好友
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    if(new AddFriend().addFriend(msg.getUser())){
                        Message responseMsg = new Message();
                        responseMsg.setType("addfriendac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("checkuser")){//检查添加好友是否存在
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    boolean flag=false;
                    flag=new CheckUser().checkUser(msg.getUser());
                    System.out.println("r"+flag);
                    if(flag){
                        Message responseMsg = new Message();
                        responseMsg.setType("checkuserac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }
                    else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("eachother")){//检查当前好友关系状态
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    int f = -1,ff=-1;
                    f = new CheckIsFriend().checkIsFriend(msg.getUser());
                    ff = new CheckIsFriend2().checkIsFriend(msg.getUser());
                    System.out.println("y" + f + " " + "ff");
                    if(f==1||ff==1){
                        Message responseMsg = new Message();
                        responseMsg.setType("eachotherac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else if(f==0){
                        Message responseMsg = new Message();
                        responseMsg.setType("eachotherf");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else if(ff==0){
                        Message responseMsg = new Message();
                        responseMsg.setType("eachotherff");
                        new UpdateFriend().updateFriend(msg.getUser().getFriendid(),msg.getUser().getId(),1);
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("grouprelation")){//检查发送入群请求
                    Boolean fflag=new AddGroup().existGroup(msg.getGroup());
                    if(fflag==false){
                        Message responseMsg = new Message();
                        responseMsg.setType("nothisgroup");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        int flag=new AddGroup().getState(msg.getUser().getId(),msg.getGroup().getGroupid());
                        if(flag==-1){
                            Message responseMsg = new Message();
                            if(new AddGroup().addGroup(msg.getGroup())){
                                responseMsg.setType("grouprelationac");}
                            responseMsg.setUser(msg.getUser());
                            oos.writeObject(responseMsg);
                        }else if(flag==0){
                            Message responseMsg = new Message();
                            responseMsg.setType("sendedac");
                            responseMsg.setUser(msg.getUser());
                            oos.writeObject(responseMsg);
                        }else if(flag==1||flag==2){
                            Message responseMsg = new Message();
                            responseMsg.setType("belonggroupac");
                            responseMsg.setUser(msg.getUser());
                            oos.writeObject(responseMsg);
                        }else{
                            oos.writeObject(msg);
                        } }
                } else if(msg.getType().equals("getfriends")){//获得好友列表
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    ArrayList<User> flist=new GetFriends().getFriends(msg.getUser());
                    for(int i=0;i<flist.size();i++){
                        User tmpuser=flist.get(i);
                        tmpuser=new GetUser().getUser(tmpuser);
                        if(ManageSocket.getOos(tmpuser.getId())!=null)
                        {
                            tmpuser.setState(1);
                        }else{
                            tmpuser.setState(0);
                        }
                        flist.set(i,tmpuser);
                    }
                    msg.setFriendList(flist);
                    if(msg.getFriendList().size()!=0){
                        Message responseMsg = new Message();
                        responseMsg.setType("getfriendsac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setFriendList(flist);
                        oos.writeObject(responseMsg);
                    }
                    else{
                        Message responseMsg = new Message();
                        responseMsg.setType("getfriendsac");
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("getgroups")){//获得群聊列表
                    //System.out.println(msg.getGroup().getGroupname()+"得到了");
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    ArrayList<Group>flist=new GetFriends().getGroups(msg.getGroup());
                    for(int i=0;i<flist.size();i++){
                        Group tmpuser=flist.get(i);
                        tmpuser=new GetGroup().getGroup(tmpuser);
                        flist.set(i,tmpuser);
                    }
                    msg.setGroupList(flist);
                    if(msg.getGroupList().size()!=0){
                        Message responseMsg = new Message();
                        responseMsg.setType("getgroupsac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setGroupList(flist);
                        oos.writeObject(responseMsg);
                    } else{
                        Message responseMsg = new Message();
                        responseMsg.setType("getgroupsac");
                        oos.writeObject(responseMsg);
                    }
                } else if(msg.getType().equals("getothergroups")){//获得我加入的群聊列表
                    //System.out.println(msg.getGroup().getGroupname()+"有得到了");
                    ArrayList<Group>flist=new GetFriends().getotherGroups(msg.getGroup());
                    for(int i=0;i<flist.size();i++){
                        Group tmpuser=flist.get(i);
                        tmpuser=new GetGroup().getGroup(tmpuser);
                        //System.out.println(tmpuser.getGroupname()+"彭润泽");
                        flist.set(i,tmpuser);
                    }
                    //msg.setGlist(flist);
                    if(flist.size()!=0){
                        Message responseMsg = new Message();
                        responseMsg.setType("getothergroupsac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setOwngroupList(flist);
                        oos.writeObject(responseMsg);
                    } else{
                        Message responseMsg = new Message();
                        responseMsg.setType("getothergroupsac");
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("getinformations")){//获得消息列表
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    ArrayList<Information>flist=new GetInformation().getInformation(msg.getUser());
                    for(int i=0;i<flist.size();i++){
                        User tmpuser=new User();
                        tmpuser.setId(flist.get(i).getUserid());
                        tmpuser.setHeadimage(flist.get(i).getUserimage());
                        tmpuser=new GetUser().getUser(tmpuser);
                        Information im=new Information();
                        im=flist.get(i);
                        im.setUsername(tmpuser.getName());
                        im.setUserimage(tmpuser.getHeadimage());
                        flist.set(i,im);
                    }
                    msg.setIlist(flist);
                    if(msg.getIlist().size()!=0){
                        Message responseMsg = new Message();
                        responseMsg.setType("getinformationsac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setIlist(flist);
                        oos.writeObject(responseMsg);
                    }
                    else{
                        Message responseMsg = new Message();
                        responseMsg.setType("getinformationsac");
                        //responseMsg.setUser(msg.getUser());
                        //responseMsg.setIlist(flist);
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("getgroupinformations")){//获得群聊验证消息列表
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    ArrayList<Group>flist=new GetFriends().getGroups(msg.getGroup());
                    ArrayList<Information>ilist=new ArrayList<Information>();
                    for(int i=0;i<flist.size();i++){
                        Group tmpgp=new Group();
                        tmpgp=flist.get(i);
                        ilist=new GetInformation().getGroupinformation(tmpgp.getGroupid(),ilist);
                    }
                    for(int i=0;i<ilist.size();i++){
                        Information newimt=ilist.get(i);
                        User tmpuser=new User();
                        tmpuser.setId(newimt.getUserid());
                        tmpuser=new GetUser().getUser(tmpuser);
                        newimt.setUserimage(tmpuser.getHeadimage());
                        newimt.setUsername(tmpuser.getName());
                        Group tmpgroup=new Group();
                        tmpgroup.setGroupid(newimt.getGroupid());
                        tmpgroup=new GetGroup().getGroup(tmpgroup);
                        newimt.setGroupimage(tmpgroup.getGroupheadimage());
                        newimt.setGroupname(tmpgroup.getGroupname());
                    }
                    msg.setQilist(ilist);
                    if(ilist.size()!=0){
                        Message responseMsg = new Message();
                        responseMsg.setType("getgroupinformationsac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setQilist(ilist);
                        oos.writeObject(responseMsg);
                    }
                    else{
                        Message responseMsg = new Message();
                        responseMsg.setType("getgroupinformationsac");
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("deletefriend")){//删除好友
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    if(new DeleteFriend().deleteFriend(msg.getUser().getId(),msg.getUser().getFriendid())){
                        Message responseMsg = new Message();
                        responseMsg.setType("deletefriendac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("acceptfriend")){//接受好友请求
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    if(new UpdateFriend().updateFriend(msg.getUser().getId(),msg.getUser().getFriendid(),1)){
                        Message responseMsg = new Message();
                        responseMsg.setType("acceptfriendac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("acceptfalsefriend")){//拒绝好友请求
                    if(new DeleteFriend().deleteFriend(msg.getUser().getId(),msg.getUser().getFriendid())){
                        Message responseMsg = new Message();
                        responseMsg.setType("acceptfalsefriendac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("gapplytrue")){//接受入群申请
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    if(new UpdateGroup().applyGroupmember(msg.getGroup().getuserid(),msg.getGroup().getGroupid(),msg.getGroup().getGroupstate())){
                        Message responseMsg = new Message();
                        responseMsg.setType("gapplytrueac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("gapplyflase")){//拒绝入群申请
                    if(new ExitGroup().exitGroup(msg.getGroup().getuserid(),msg.getGroup().getGroupid())){
                        Message responseMsg = new Message();
                        responseMsg.setType("gapplyflaseac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("addgroup")){//添加群聊
                    //ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    if(new AddGroup().addGroup(msg.getGroup())){
                        Message responseMsg = new Message();
                        responseMsg.setType("addgroupac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }

                }else if(msg.getType().equals("creatgroup")) {//创建群聊
                    Group tmpuser=msg.getGroup();
                    //System.out.println(msg.getGroup().getGroupname());
                    //System.out.println(msg.getGroup().getGroupstate());
                    if(msg.getHeadimage()!=null) {
                        //目标路径
                        String filePath = "E:/untitled/src/view/photo/";
                        File upfile = new File(filePath);
                        //如果文件目录不存在，就执行创建
                        if (!upfile.isDirectory()) {
                            upfile.mkdirs();
                        }
                        File localFile = msg.getHeadimage();
                        String filename = new GetId().getId();
                        //目标文件名称
                        String targetName = filename + ".jpg";
                        System.out.println(targetName);
                        //创建目标文件
                        File targetFile = new File(filePath + targetName);
                        FileOutputStream fos = new FileOutputStream(targetFile);
                        //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
                        //获取本地文件输入流
                        InputStream stream = new FileInputStream(localFile);
                        //写入目标文件，字节流
                        byte[] buffer = new byte[1024 * 1024];
                        int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
                        while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
                            //把字节转成String 从0到N变成String
                            fos.write(buffer, 0, byteRead);
                            fos.flush();
                        }
                        //一定要关闭文件流。
                        fos.close();
                        stream.close();
                        System.out.println("关闭上传群头像的文件流");
                        tmpuser.setGroupheadimage(filePath+targetName);
                    }else{
                        tmpuser.setGroupheadimage("");
                    }
                    //System.out.println(tmpuser.getGroupphoto());
                    msg.setGroup(tmpuser);
                    //System.out.println(msg.getGroup().getGroupid());
                    if(new UpdateGroup().creatGroup(msg.getGroup()) && new UpdateGroup().addGroupmember(msg.getGroup())){
                        Message responseMsg = new Message();
                        responseMsg.setType("creatgroupac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("exitgroup")){//退群
                    if(new ExitGroup().exitGroup(msg.getGroup().getuserid(),msg.getGroup().getGroupid())){
                        Message responseMsg = new Message();
                        responseMsg.setType("exitgroupac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("dissolutiongroup")){//解散群聊
                    System.out.println(msg.getGroup().getGroupid());
                    if(new ExitGroup().dissolutionGroup(msg.getGroup().getGroupid())){
                        Message responseMsg = new Message();
                        responseMsg.setType("dissolutiongroupac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("grouprecords")){//群聊聊天记录
                    ArrayList<Communication>flist=new ChatRecords().groupchatRecords(msg.getCommunication());
                    //msg.setFclist(flist);
                    if(flist.size()!=0){
                        for(int i=0;i<flist.size();i++){
                            User sender=flist.get(i).getSender();
                            sender=new GetUser().getUser(sender);
                            Communication co=flist.get(i);
                            co.setSender(sender);
                            flist.set(i,co);
                        }
                        flist.sort(Comparator.comparing(Communication::getSendDateTime));
                        Message responseMsg = new Message();
                        responseMsg.setType("grouprecordsac");
                        responseMsg.setQclist(flist);
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setType("grouprecordsac");
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("groupchatsend")){//群聊转发消息请求
                    //System.out.println("hhh");
                    if(new ChatSend().groupChatsend(msg.getCommunication())){
                        //System.out.println(msg.getCommunication().getGrouper().getGroupid());
                        ArrayList<Group> list = new ChatSend().getGroupmember(msg.getCommunication().getGroup());
                        //System.out.println("groupchat"+list.size());
                        for(int i=0; i<list.size(); i++) {
                            Message responseMsg = new Message();
                            responseMsg.setType("groupchatsendac");
                            responseMsg.setCommunication(msg.getCommunication());
                            //oos.writeObject(responseMsg);
                            //System.out.println(ManageSocket.getOos(msg.getCommunication().getReceiver().getUserid()));
                            if (ManageSocket.getOos(list.get(i).getuserid()) != null) {
                                System.out.println(list.get(i).getuserid());
                                oos = ManageSocket.getOos(list.get(i).getuserid());
                                oos.writeObject(responseMsg);
                            } else {//不在线

                            }
                        }
                        oos = ManageSocket.getOos(msg.getCommunication().getSender().getId());
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setCommunication(msg.getCommunication());
                        responseMsg.setType("groupchatsendac");
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("records")){//私聊聊天记录
                    ArrayList<Communication>flist=new ChatRecords().chatRecords(msg.getCommunication());
                    //msg.setFclist(flist);
                    if(flist.size()!=0){
                        for(int i=0;i<flist.size();i++){
                            User sender=flist.get(i).getSender();
                            sender=new GetUser().getUser(sender);
                            Communication co=flist.get(i);
                            co.setSender(sender);
                            flist.set(i,co);
                        }
                        flist.sort(Comparator.comparing(Communication::getSendDateTime));

                        Message responseMsg = new Message();
                        responseMsg.setType("recordsac");
                        responseMsg.setFclist(flist);
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setType("recordsac");
                        //responseMsg.setFclist(flist);
                        //responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("chatsend")){//私聊转发消息请求
                    if(new ChatSend().chatSend(msg.getCommunication())){
                        Message responseMsg = new Message();
                        responseMsg.setType("chatsendac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setCommunication(msg.getCommunication());
                        oos.writeObject(responseMsg);
                        System.out.println(ManageSocket.getOos(msg.getCommunication().getReceiver().getId()));
                        if(ManageSocket.getOos(msg.getCommunication().getReceiver().getId())!=null){
                            oos=ManageSocket.getOos(msg.getCommunication().getReceiver().getId());
                            oos.writeObject(responseMsg);
                            oos=ManageSocket.getOos(msg.getCommunication().getSender().getId());
                        }else{//不在线

                        }
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setType("chatsendac");
                        responseMsg.setCommunication(msg.getCommunication());
                        oos.writeObject(responseMsg);
                    }
                } else if(msg.getType().equals("getmembers")){//获得群成员
                    ArrayList<Group> glist = new ChatSend().getGroupmember(msg.getGroup());
                    ArrayList<User> flist = new ChatSend().getGroupmembers(msg.getGroup().getGroupid());
                    for(int i=0;i<glist.size();i++){
                        Group tmpuser=glist.get(i);
                        tmpuser=new GetMembers().getMembers(tmpuser);
                        User user = new User();
                        user.setId(glist.get(i).getuserid());
                        User uuser = new GetUser().getUser(user);
                        glist.set(i,tmpuser);
                        flist.set(i,uuser);
                    }
                    msg.setQmslist(glist);
                    msg.setQmlist(flist);
                    Message responseMsg = new Message();
                    responseMsg.setType("getmembersac");
                    responseMsg.setUser(msg.getUser());
                    responseMsg.setQmslist(glist);
                    responseMsg.setQmlist(flist);
                    oos.writeObject(responseMsg);
                }else if(msg.getType().equals("groupinformation")){//修改群信息
                    Group group = new GetGroup().getGroup(msg.getGroup());
                    if(new ChatSend().checkGroupowner(msg.getGroup())){
                        Message responseMsg = new Message();
                        responseMsg.setType("groupinformationac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setGroup(group);
                        oos.writeObject(responseMsg);
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setType("groupinformationwa");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setGroup(group);
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("changeGroupInformation")){//修改群信息
                    //System.out.println(msg.getUser());
                    Group tmpgroup=msg.getGroup();
                    if(msg.getHeadimage()!=null) {
                        //目标路径
                        String filePath = "E:/untitled/src/view/photo/";
                        File upfile = new File(filePath);
                        //如果文件目录不存在，就执行创建
                        if (!upfile.isDirectory()) {
                            upfile.mkdirs();
                        }
                        File localFile = msg.getHeadimage();
                        String filename = new GetId().getId();
                        //目标文件名称
                        String targetName = filename + ".jpg";
                        System.out.println(targetName);
                        //创建目标文件
                        File targetFile = new File(filePath + targetName);
                        FileOutputStream fos = new FileOutputStream(targetFile);
                        //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
                        //获取本地文件输入流
                        InputStream stream = new FileInputStream(localFile);
                        //写入目标文件，字节流
                        byte[] buffer = new byte[1024 * 1024];
                        int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
                        while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
                            //把字节转成String 从0到N变成String
                            fos.write(buffer, 0, byteRead);
                            fos.flush();
                        }
                        //一定要关闭文件流。
                        fos.close();
                        stream.close();
                        System.out.println("关闭上传头像的文件流");
                        tmpgroup.setGroupheadimage(filePath+targetName);
                        msg.setGroup(tmpgroup);
                    }
                    System.out.println(msg.getGroup().getGroupheadimage());
                    if(new UpdateInformation().updateGroupInformation(msg.getGroup())){
                        Message responseMsg = new Message();
                        responseMsg.setType("changeGroupInformationac");
                        responseMsg.setUser(msg.getUser());
                        oos.writeObject(responseMsg);
                    }else{
                        oos.writeObject(msg);
                    }
                }else if(msg.getType().equals("sendphoto")){//发送图片
                    Communication conn = msg.getCommunication();
                    //目标路径
                    String filePath = "E:/untitled/src/view/photo/";
                    File upfile = new File(filePath);
                    //如果文件目录不存在，就执行创建
                    if (!upfile.isDirectory()) {
                        upfile.mkdirs();
                    }
                    File localFile = msg.getHeadimage();
                    String filename = new GetId().getId();
                    //目标文件名称
                    String targetName = filename + ".jpg";
                    System.out.println(targetName);
                    //创建目标文件
                    File targetFile = new File(filePath + targetName);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
                    //获取本地文件输入流
                    InputStream stream = new FileInputStream(localFile);
                    //写入目标文件，字节流
                    byte[] buffer = new byte[1024 * 1024];
                    int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
                    while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
                        //把字节转成String 从0到N变成String
                        fos.write(buffer, 0, byteRead);
                        fos.flush();
                    }
                    //一定要关闭文件流。
                    fos.close();
                    stream.close();
                    System.out.println("关闭上传头像的文件流");
                    conn.setContent(filePath+targetName);
                    msg.setCommunication(conn);
                    if(new ChatSend().chatSend(msg.getCommunication())) {
                        Message responseMsg = new Message();
                        responseMsg.setType("sendphotoac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setCommunication(msg.getCommunication());
                        oos.writeObject(responseMsg);
                        System.out.println(ManageSocket.getOos(msg.getCommunication().getReceiver().getId()));
                        if (ManageSocket.getOos(msg.getCommunication().getReceiver().getId()) != null) {
                            oos = ManageSocket.getOos(msg.getCommunication().getReceiver().getId());
                            oos.writeObject(responseMsg);
                            oos = ManageSocket.getOos(msg.getCommunication().getSender().getId());
                        } else {//不在线

                        }
                    }
                }else if(msg.getType().equals("sendfile")){//发送文件
                    Communication conn = msg.getCommunication();
                    //目标路径
                    String filePath = "E:/untitled/src/view/photo/";
                    File upfile = new File(filePath);
                    //如果文件目录不存在，就执行创建
                    if (!upfile.isDirectory()) {
                        upfile.mkdirs();
                    }
                    File localFile = msg.getHeadimage();
                    String filename = new GetId().getId();
                    //目标文件名称
                    String targetName = filename + ".doc";
                    System.out.println(targetName);
                    //创建目标文件
                    File targetFile = new File(filePath + targetName);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
                    //获取本地文件输入流
                    InputStream stream = new FileInputStream(localFile);
                    //写入目标文件，字节流
                    byte[] buffer = new byte[1024 * 1024];
                    int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
                    while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
                        //把字节转成String 从0到N变成String
                        fos.write(buffer, 0, byteRead);
                        fos.flush();
                    }
                    //一定要关闭文件流。
                    fos.close();
                    stream.close();
                    System.out.println("关闭上传文件的文件流");
                    conn.setContent(filePath+targetName);
                    msg.setCommunication(conn);
                    if(new ChatSend().chatSend(msg.getCommunication())) {
                        Message responseMsg = new Message();
                        responseMsg.setType("sendfileac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setCommunication(msg.getCommunication());
                        oos.writeObject(responseMsg);
                        System.out.println(ManageSocket.getOos(msg.getCommunication().getReceiver().getId()));
                        if (ManageSocket.getOos(msg.getCommunication().getReceiver().getId()) != null) {
                            oos = ManageSocket.getOos(msg.getCommunication().getReceiver().getId());
                            oos.writeObject(responseMsg);
                            oos = ManageSocket.getOos(msg.getCommunication().getSender().getId());
                        } else {//不在线

                        }
                    }
                }else if(msg.getType().equals("sendgroupphoto")){//群聊发送图片
                    Communication conn = msg.getCommunication();
                    //目标路径
                    String filePath = "E:/untitled/src/view/photo/";
                    File upfile = new File(filePath);
                    //如果文件目录不存在，就执行创建
                    if (!upfile.isDirectory()) {
                        upfile.mkdirs();
                    }
                    File localFile = msg.getHeadimage();
                    String filename = new GetId().getId();
                    //目标文件名称
                    String targetName = filename + ".jpg";
                    System.out.println(targetName);
                    //创建目标文件
                    File targetFile = new File(filePath + targetName);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
                    //获取本地文件输入流
                    InputStream stream = new FileInputStream(localFile);
                    //写入目标文件，字节流
                    byte[] buffer = new byte[1024 * 1024];
                    int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
                    while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
                        //把字节转成String 从0到N变成String
                        fos.write(buffer, 0, byteRead);
                        fos.flush();
                    }
                    //一定要关闭文件流。
                    fos.close();
                    stream.close();
                    System.out.println("关闭上传头像的文件流");
                    conn.setContent(filePath+targetName);
                    msg.setCommunication(conn);
                    if(new ChatSend().groupChatsend(msg.getCommunication())){
                        //System.out.println(msg.getCommunication().getGrouper().getGroupid());
                        ArrayList<Group> list = new ChatSend().getGroupmember(msg.getCommunication().getGroup());
                        //System.out.println("groupchat"+list.size());
                        for(int i=0; i<list.size(); i++) {
                            Message responseMsg = new Message();
                            responseMsg.setType("sendgroupphotoac");
                            responseMsg.setCommunication(msg.getCommunication());
                            //oos.writeObject(responseMsg);
                            //System.out.println(ManageSocket.getOos(msg.getCommunication().getReceiver().getUserid()));
                            if (ManageSocket.getOos(list.get(i).getuserid()) != null) {
                                System.out.println(list.get(i).getuserid());
                                oos = ManageSocket.getOos(list.get(i).getuserid());
                                oos.writeObject(responseMsg);
                            } else {//不在线

                            }
                        }
                        oos = ManageSocket.getOos(msg.getCommunication().getSender().getId());
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setCommunication(msg.getCommunication());
                        responseMsg.setType("sendgroupphotoac");
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("sendgroupfile")){//群聊发送文件
                    Communication conn = msg.getCommunication();
                    //目标路径
                    String filePath = "E:/untitled/src/view/photo/";
                    File upfile = new File(filePath);
                    //如果文件目录不存在，就执行创建
                    if (!upfile.isDirectory()) {
                        upfile.mkdirs();
                    }
                    File localFile = msg.getHeadimage();
                    String filename = new GetId().getId();
                    //目标文件名称
                    String targetName = filename + ".doc";
                    System.out.println(targetName);
                    //创建目标文件
                    File targetFile = new File(filePath + targetName);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
                    //获取本地文件输入流
                    InputStream stream = new FileInputStream(localFile);
                    //写入目标文件，字节流
                    byte[] buffer = new byte[1024 * 1024];
                    int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
                    while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
                        //把字节转成String 从0到N变成String
                        fos.write(buffer, 0, byteRead);
                        fos.flush();
                    }
                    //一定要关闭文件流。
                    fos.close();
                    stream.close();
                    System.out.println("关闭上传文件的文件流");
                    conn.setContent(filePath+targetName);
                    msg.setCommunication(conn);
                    if(new ChatSend().groupChatsend(msg.getCommunication())){
                        //System.out.println(msg.getCommunication().getGrouper().getGroupid());
                        ArrayList<Group> list = new ChatSend().getGroupmember(msg.getCommunication().getGroup());
                        //System.out.println("groupchat"+list.size());
                        for(int i=0; i<list.size(); i++) {
                            Message responseMsg = new Message();
                            responseMsg.setType("sendgroupfileac");
                            responseMsg.setCommunication(msg.getCommunication());
                            //oos.writeObject(responseMsg);
                            //System.out.println(ManageSocket.getOos(msg.getCommunication().getReceiver().getUserid()));
                            if (ManageSocket.getOos(list.get(i).getuserid()) != null) {
                                System.out.println(list.get(i).getuserid());
                                oos = ManageSocket.getOos(list.get(i).getuserid());
                                oos.writeObject(responseMsg);
                            } else {//不在线

                            }
                        }
                        oos = ManageSocket.getOos(msg.getCommunication().getSender().getId());
                    }else{
                        Message responseMsg = new Message();
                        responseMsg.setCommunication(msg.getCommunication());
                        responseMsg.setType("sendgroupfileac");
                        oos.writeObject(responseMsg);
                    }
                }else if(msg.getType().equals("emoji")){
                    if(new ChatSend().chatSend(msg.getCommunication())) {
                        Message responseMsg = new Message();
                        responseMsg.setType("emojiac");
                        responseMsg.setUser(msg.getUser());
                        responseMsg.setCommunication(msg.getCommunication());
                        oos.writeObject(responseMsg);
                        System.out.println(ManageSocket.getOos(msg.getCommunication().getReceiver().getId()));
                        if (ManageSocket.getOos(msg.getCommunication().getReceiver().getId()) != null) {
                            oos = ManageSocket.getOos(msg.getCommunication().getReceiver().getId());
                            oos.writeObject(responseMsg);
                            oos = ManageSocket.getOos(msg.getCommunication().getSender().getId());
                        } else {//不在线

                        }
                    }
                }else if(msg.getType().equals("groupemoji")){
                    if(new ChatSend().groupChatsend(msg.getCommunication())){
                        ArrayList<Group> list = new ChatSend().getGroupmember(msg.getCommunication().getGroup());
                        for(int i=0; i<list.size(); i++) {
                            Message responseMsg = new Message();
                            responseMsg.setType("groupemojiac");
                            responseMsg.setCommunication(msg.getCommunication());
                            if (ManageSocket.getOos(list.get(i).getuserid()) != null) {
                                System.out.println(list.get(i).getuserid());
                                oos = ManageSocket.getOos(list.get(i).getuserid());
                                oos.writeObject(responseMsg);
                            } else {//不在线

                            }
                        }

                    }
                }
            }catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
